// 这里可以改变3D模型的颜色，改变某些零部件的颜色等。// src/utils/colorControl.js
import * as THREE from "three";

class ColorControl {
    constructor() {
        // 部件颜色状态管理
        this.partColors = new Map();

        // 预定义颜色
        this.colors = {
            yellow: 0xffff00,
            red: 0xff0000,
            green: 0x00ff00,
            blue: 0x0000ff,
            orange: 0xffa500,
            purple: 0x800080,
            cyan: 0x00ffff,
        };
    }

    // 创建带颜色的新材质
    createColoredMaterial(color) {
        return new THREE.MeshStandardMaterial({
            color: color,
            roughness: 0.5,
            metalness: 0.2,
        });
    }

    // 改变指定部件的颜色
    changePartColor(model, partName, color) {
        if (!model) return;

        model.traverse((child) => {
            if (
                child.isMesh &&
                child.name &&
                (child.name.toLowerCase().includes(partName.toLowerCase()) ||
                    partName.toLowerCase().includes(child.name.toLowerCase()))
            ) {
                // 保存原始材质
                if (!this.partColors.has(child.uuid)) {
                    this.partColors.set(child.uuid, {
                        originalMaterial: child.material,
                        currentColor: null,
                    });
                }

                // 应用新颜色
                const newMaterial = this.createColoredMaterial(color);
                child.material = newMaterial;
                child.material.needsUpdate = true;

                // 更新状态
                const state = this.partColors.get(child.uuid);
                state.currentColor = color;
                this.partColors.set(child.uuid, state);

                console.log(`部件 ${child.name} 已变色为 #${color.toString(16)}`);
            }
        });
    }

    // 批量改变多个部件颜色
    changeMultiplePartsColor(model, partsConfig) {
        if (!model) return;

        Object.keys(partsConfig).forEach((partName) => {
            const color = partsConfig[partName];
            this.changePartColor(model, partName, color);
        });
    }

    // 恢复部件原始颜色
    restorePartColor(model, partName) {
        if (!model) return;

        model.traverse((child) => {
            if (
                child.isMesh &&
                child.name &&
                (child.name.toLowerCase().includes(partName.toLowerCase()) ||
                    partName.toLowerCase().includes(child.name.toLowerCase()))
            ) {
                const state = this.partColors.get(child.uuid);
                if (state && state.originalMaterial) {
                    child.material = state.originalMaterial;
                    child.material.needsUpdate = true;
                    state.currentColor = null;
                    this.partColors.set(child.uuid, state);

                    console.log(`部件 ${child.name} 已恢复原始颜色`);
                }
            }
        });
    }

    // 恢复所有部件颜色
    restoreAllPartsColor(model) {
        if (!model) return;

        model.traverse((child) => {
            if (child.isMesh && this.partColors.has(child.uuid)) {
                const state = this.partColors.get(child.uuid);
                if (state && state.originalMaterial) {
                    child.material = state.originalMaterial;
                    child.material.needsUpdate = true;
                }
            }
        });

        // 清空颜色状态
        this.partColors.clear();
        console.log("所有部件颜色已恢复");
    }

    // 切换部件颜色
    togglePartColor(model, partName, color) {
        let isColored = false;
        if (!model) return;

        model.traverse((child) => {
            if (
                child.isMesh &&
                child.name &&
                (child.name.toLowerCase().includes(partName.toLowerCase()) ||
                    partName.toLowerCase().includes(child.name.toLowerCase()))
            ) {
                const state = this.partColors.get(child.uuid);
                if (state && state.currentColor) {
                    isColored = true;
                }
            }
        });

        if (isColored) {
            this.restorePartColor(model, partName);
        } else {
            this.changePartColor(model, partName, color);
        }
    }

    // 获取颜色控制实例
    static getInstance() {
        if (!this.instance) {
            this.instance = new ColorControl();
        }
        return this.instance;
    }
}

// 导出单例实例
export default ColorControl.getInstance();